package pe.edu.upla.lamerced.desk.view.maestro;

import java.util.List;
import javax.swing.JDesktopPane;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import org.apache.log4j.Logger;
import pe.edu.upla.lamerced.Inicial;
import pe.edu.upla.lamerced.bean.Categoria;
import pe.edu.upla.lamerced.bean.Detcategoria;
import pe.edu.upla.lamerced.service.CategoriaService;

/**
 *
 * @author elvis
 */
public class ListaDetalleCategoria extends javax.swing.JInternalFrame {

  private static final Logger logger = Logger.getLogger(ListaDetalleCategoria.class);

   private JDesktopPane escritorio;
   private Detcategoria detcategoria;
   private CategoriaService categoriaService;
   private List<Categoria> listaCategorias;
   private List<Detcategoria> listaDetalleCategoria;

    /** Creates new form ListaCategoria */
    public ListaDetalleCategoria() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
   // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
   private void initComponents() {

      jScrollPane1 = new javax.swing.JScrollPane();
      tblCategorias = new javax.swing.JTable();
      jButton1 = new javax.swing.JButton();
      jButton2 = new javax.swing.JButton();
      jButton3 = new javax.swing.JButton();
      jLabel2 = new javax.swing.JLabel();
      jPanel1 = new javax.swing.JPanel();
      jLabel1 = new javax.swing.JLabel();
      cbCategoria = new javax.swing.JComboBox();
      jButton4 = new javax.swing.JButton();
      jButton5 = new javax.swing.JButton();

      setClosable(true);
      setTitle("Lista Categorias");

      tblCategorias.setModel(new javax.swing.table.DefaultTableModel(
         new Object [][] {
            {null, null, null}
         },
         new String [] {
            "Codigo", "Categoria", "Nombre"
         }
      ));
      jScrollPane1.setViewportView(tblCategorias);
      tblCategorias.getColumnModel().getColumn(1).setResizable(false);

      jButton1.setText("Nuevo");
      jButton1.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton1ActionPerformed(evt);
         }
      });

      jButton2.setText("Modificar");
      jButton2.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton2ActionPerformed(evt);
         }
      });

      jButton3.setText("Eliminar");
      jButton3.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton3ActionPerformed(evt);
         }
      });

      jLabel2.setFont(new java.awt.Font("Tahoma", 1, 14));
      jLabel2.setText("Equipamiento por Categoria");

      jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

      jLabel1.setText("Categoria");

      cbCategoria.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

      jButton4.setText("R");
      jButton4.setToolTipText("Aplicar Filtro");
      jButton4.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton4ActionPerformed(evt);
         }
      });

      jButton5.setText("T");
      jButton5.setToolTipText("Recargar Lista Completa");
      jButton5.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton5ActionPerformed(evt);
         }
      });

      javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
      jPanel1.setLayout(jPanel1Layout);
      jPanel1Layout.setHorizontalGroup(
         jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(jPanel1Layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jLabel1)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(cbCategoria, javax.swing.GroupLayout.PREFERRED_SIZE, 173, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
      );
      jPanel1Layout.setVerticalGroup(
         jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
            .addContainerGap(26, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
               .addComponent(jLabel1)
               .addComponent(cbCategoria, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
               .addComponent(jButton4)
               .addComponent(jButton5))
            .addGap(22, 22, 22))
      );

      jButton5.getAccessibleContext().setAccessibleDescription("Recargar Lista completa");

      javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
      getContentPane().setLayout(layout);
      layout.setHorizontalGroup(
         layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(layout.createSequentialGroup()
            .addGap(27, 27, 27)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(27, Short.MAX_VALUE))
         .addGroup(layout.createSequentialGroup()
            .addGap(53, 53, 53)
            .addComponent(jButton1)
            .addGap(53, 53, 53)
            .addComponent(jButton2)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 56, Short.MAX_VALUE)
            .addComponent(jButton3)
            .addGap(53, 53, 53))
         .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 422, Short.MAX_VALUE)
         .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap(111, Short.MAX_VALUE)
            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 225, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(86, 86, 86))
      );
      layout.setVerticalGroup(
         layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jLabel2)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
               .addComponent(jButton1)
               .addComponent(jButton2)
               .addComponent(jButton3))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 164, Short.MAX_VALUE))
      );

      pack();
   }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
       if (tblCategorias.getSelectedRow()==-1){
          JOptionPane.showMessageDialog(this, "No ha seleccionado ningun equipamiento para modificar");
         return;
       }
       FormDetCategoria form = new FormDetCategoria();
       form.getLblCategoria().setEnabled(false);
       form.setEscritorio(escritorio);
       form.setDetcategoria(listaDetalleCategoria.get(tblCategorias.getSelectedRow()));
       form.cargaInicial();
       escritorio.add(form);
       form.setVisible(true);
       this.dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
       // form categoria
       FormDetCategoria form = new FormDetCategoria();
       form.setEscritorio(escritorio);
       form.setDetcategoria(new Detcategoria());
       form.cargaInicial();
       escritorio.add(form);
       form.setVisible(true);
       this.dispose();
       this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
       if (tblCategorias.getSelectedRow()==-1){
         JOptionPane.showMessageDialog(this, "No ha seleccionado ningun equipamiento");
         return;
       }
       int res = JOptionPane.showConfirmDialog(this,"Seguro que desea eliminar el equipamiento?","Eliminar Equipamiento",JOptionPane.YES_NO_OPTION);
       if(res==0){
          Detcategoria dt = listaDetalleCategoria.get(tblCategorias.getSelectedRow());
          dt.setEstado(0);
         categoriaService.registrarDetalleCategoria(dt);
         JOptionPane.showMessageDialog(this, "se ha eliminado el equipamiento seleccionado");
         listaDetalleCategoria = categoriaService.listardetalleCategorias(listaCategorias.get(cbCategoria.getSelectedIndex()).getIdcategoria());
      pintagrilla();
       }
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
      listaDetalleCategoria = categoriaService.listardetalleCategorias(listaCategorias.get(cbCategoria.getSelectedIndex()).getIdcategoria());
      pintagrilla();
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
       listaDetalleCategoria = categoriaService.listardetalleCategorias();
       pintagrilla();
    }//GEN-LAST:event_jButton5ActionPerformed


   // Variables declaration - do not modify//GEN-BEGIN:variables
   private javax.swing.JComboBox cbCategoria;
   private javax.swing.JButton jButton1;
   private javax.swing.JButton jButton2;
   private javax.swing.JButton jButton3;
   private javax.swing.JButton jButton4;
   private javax.swing.JButton jButton5;
   private javax.swing.JLabel jLabel1;
   private javax.swing.JLabel jLabel2;
   private javax.swing.JPanel jPanel1;
   private javax.swing.JScrollPane jScrollPane1;
   private javax.swing.JTable tblCategorias;
   // End of variables declaration//GEN-END:variables

   public JDesktopPane getEscritorio() {
      return escritorio;
   }
   public void setEscritorio(JDesktopPane escritorio) {
      this.escritorio = escritorio;
   }

   public void cargainicial(){
      if(categoriaService==null){
         categoriaService = (CategoriaService)Inicial.getFactory().getBean("categoriaService");
      }
      System.out.println("lista categorias");
      if(listaCategorias==null){
         listaCategorias = categoriaService.listarCategorias();
      }
      cbCategoria.removeAllItems();
      for(Categoria cat : listaCategorias){
         cbCategoria.addItem(cat.getNombre());
      }
      System.out.println("lista equipamiento categoria");
      if(listaDetalleCategoria==null){
         listaDetalleCategoria = categoriaService.listardetalleCategorias();
      }
      pintagrilla();
   }

   public void pintagrilla(){
        DefaultTableModel tabla = (DefaultTableModel) tblCategorias.getModel();
        tabla.setRowCount(0);
        for (Detcategoria equip : listaDetalleCategoria) {
            Object[] fila = new Object[4];
            fila[0] = equip.getIddetaleCategoria();
            fila[1] = equip.getCategoria().getNombre();
            fila[2] = equip.getNombre();
            tabla.addRow(fila);
        }

      }
   
}
